<?php
/*
 * Copyright (c) 2013 Eugene Kuzmenko
 *
 * Permission  is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in  the Software without restriction, including without limitation the rights
 * to  use,  copy,  modify,  merge, publish, distribute, sublicense, and/or sell
 * copies  of  the  Software,  and  to  permit  persons  to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The  above  copyright  notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE  SOFTWARE  IS  PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED,  INCLUDING  BUT  NOT  LIMITED  TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS  FOR  A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS  OR  COPYRIGHT  HOLDERS  BE  LIABLE  FOR  ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * For more information, see <http://www.aljey.com>.
 */

namespace Al\HTTP\Lib;

use Al\Core\Lib\Event,
    Al\Core\Lib\Service,
    Al\HTTP\Lib\Vars\Cookie,
    Al\HTTP\Lib\Vars\Params,
    Al\HTTP\Lib\Vars\Post,
    Al\HTTP\Lib\Vars\Query,
    Al\HTTP\Lib\Vars\Server,
    Al\HTTP\Lib\Vars\Session,
    Al\HTTP\Lib\Vars\Files;

/**
 * Performs intelligent initialization of the plugin.
 *
 * @author Eugene Kuzmenko <mail@aljey.com>
 */
class Extension {

  /**
   * Executed automatically by the kernel during bootstrap.
   * @param Service $service
   */
  public function load(Service $service) {
    $service['request'] = function () { return new Request; };
    $service['http'] = function () {
      return new Service(array(
        'cookie' => function () { return new Cookie; },
        'params' => function () { return new Params; },
        'post' => function () { return new Post; },
        'query' => function () { return new Query; },
        'server' => function () { return new Server; },
        'session' => function () { return new Session; },
        'files' => function () { return new Files; },
      ));
    };
    $service['mime'] = function (Service $service) {
      global $mime;
      return new Service(array(
        'type' => function () use (&$mime) {
          return $mime;
        },
        'ext' => function () use (&$service, &$mime) {
          $cache = $service['cache'];
          if (!$cache['mime']) $cache['mime'] = array_flip($mime);
          return $cache['mime'];
        },
      ));
    };
    $service->when('event', function (Event $event) {
      $event->on('404', function () {
        header(Service::get('http.server.SERVER_PROTOCOL') . ' 404 Not Found');
        header('Status: 404 Not Found');
        $result = Service::get('template')
          ->render(Service::get('config.plugins.Al\HTTP.not_found'));
        $response = new Response($result[0]);
        $response->setAccelerate(false)->render();
      });
    });
  }

}